//
//  HHPayPopView.swift
//  HHUIKit
//
//  Created by chun.chen on 2021/9/8.
//  支付弹框

import BCComConfigKit
import BCFoundation

public class HHPayPopView: UIView {
    // MARK: - property

    /// blcok 0: 表示取消
    public var completionBlcok: ((Int) -> Void)?

    /// 数据源
    public var dataArr: [[String: Any]] = [
        ["icon": "icon_pay_wx", "name": BCLOC("微信支付"), "type": 1],
        ["icon": "icon_pay_ali", "name": BCLOC("支付宝支付"), "type": 2]]

    // MARK: - UI

    private lazy var contentView: UIView = {
       let view = UIView(frame: CGRect(x: 0, y: kBCSCREEN_HEIGHT, width: kBCSCREEN_WIDTH, height: 0))
        view.clipsToBounds = true
        return view
    }()

    /// tableview
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: self.bounds, style: .grouped)
        tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        tableView.backgroundColor = .white
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 54
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.separatorColor = BCComConfig.shared().splitColor
        tableView.showsVerticalScrollIndicator = false
        tableView.isScrollEnabled = false
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
        return tableView
    }()

    private lazy var shadeControl: UIControl = {
        let control = UIControl(frame: self.bounds)
        control.backgroundColor = UIColor(white: 0.1, alpha: 0.5)
        control.addTarget(self, action: #selector(shadeControlTouch(_:)), for: .touchUpInside)
        return control
    }()

    // MARK: - override

    override init(frame: CGRect) {
        super.init(frame: frame)
        configSubviews()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    // MARK: - class method

    @discardableResult
    public static func show(_ completion: ((Int) -> Void)?) -> HHPayPopView? {
        var tmpSupView: UIView
        guard let window = UIApplication.shared.delegate?.window else { return nil }
        tmpSupView = window!

        let popView = HHPayPopView(frame: tmpSupView.bounds)
        popView.completionBlcok = completion

        tmpSupView.addSubview(popView)
        popView.showAnimtion()
        return popView
    }

    // 内容高度
    var contentHeight: CGFloat {
        return CGFloat(54 + dataArr.count * 54 + 40 + kBCScreenSafeFitBottom)
    }

    // MARK: - public method

    func showAnimtion() {
        isHidden = false
        UIView.animate(withDuration: 0.3) {
            self.contentView.setBc_y(self.bc_bottom() - self.contentHeight)

        } completion: { _ in
            
        }
    }

    func hideAnimation() {
        if isHidden {
            return
        }
        UIView.animate(withDuration: 0.3) {
            self.contentView.setBc_y(self.bc_bottom())

        } completion: { _ in
            self.removeFromSuperview()
            self.isHidden = true
        }
    }

    // MARK: - private

    // MARK: - event

    @objc private func shadeControlTouch(_ sebder: UIControl) {
    }

    @objc private func closedButtonClicked(_ button: UIButton) {
        hideAnimation()
    }

    // MARK: - subviews

    private func configSubviews() {
        addSubview(shadeControl)
        addSubview(contentView)
        
        contentView.frame = CGRect(x: 0, y: kBCSCREEN_HEIGHT, width: kBCSCREEN_WIDTH, height: contentHeight)
        tableView.frame = CGRect(x: 0, y: 54, width: Int(kBCSCREEN_WIDTH), height:Int(contentHeight)-54)

        let closedView = UIView(frame: CGRect(x: 0, y: 0, width: kBCSCREEN_WIDTH, height: 54))
        closedView.backgroundColor = .white
        closedView.bc_add([.topLeft, .topRight], cornerRadii: CGSize(width: 16, height: 16))

        
        contentView.addSubview(closedView)
        contentView.addSubview(tableView)

        let payTitleLab = UILabel(frame: CGRect(x: 20, y: 0, width: kBCSCREEN_WIDTH, height: 54))
        payTitleLab.font = BCComConfig.shared().boldFont(16)
        payTitleLab.text = BCLOC("请选择支付方式")
        closedView.addSubview(payTitleLab)

        let closedBtn = UIButton(type: .custom)
        closedBtn.frame = CGRect(x: kBCSCREEN_WIDTH - 40, y: 12, width: 30, height: 30)
        closedBtn.setImage(UIImage(named: "nav_closed_black"), for: .normal)
        closedBtn.addTarget(self, action: #selector(closedButtonClicked(_:)), for: .touchUpInside)
        closedView.addSubview(closedBtn)
    }
}

// MARK: - delegate

extension HHPayPopView: UITableViewDelegate, UITableViewDataSource {
    // MARK: - UITableViewDelegate

    public func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr.count
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell") else {
            return UITableViewCell(style: .default, reuseIdentifier: "UITableViewCell")
        }
        let item = dataArr[indexPath.row]
        cell.imageView?.image = UIImage(named: item["icon"] as! String)
        cell.textLabel?.text = item["name"] as? String
        if indexPath.row == 0 {
            cell.accessoryView = UIImageView(image: UIImage(named: "img_selected_blue"))
        }else{
            cell.accessoryView = nil
        }
        return cell
    }

    public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.001
    }

    public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.001
    }

    public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }

    public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }

    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true)

        let item = dataArr[indexPath.row]
        if let type = item["type"] as? Int {
            completionBlcok?(type)
        }
        hideAnimation()
    }
}
